import requests
import json

# 获取动态加载页面的数据

if __name__ == "__main__":
    # 指定url
    postUrl = "http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList"
    # UA伪装
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
    }
    idList = []  # 存储企业的id
    all_data_list = []  # 存储所有的详情页的id

    # 配置参数 -参数封装
    for page in range(1,50):
        data = {
            "on": "true",
            "page": str(page),
            "pageSize": "15",
            "productName":"",
            "conditionType": "1",
            "applyname":"",
            "applysn":""
        }
        # 发送post请求
        response = requests.post(url=postUrl, headers=headers,data=data)
        # 获取响应数据
        jsonDataId = response.json()  # 字典数据 在字典数据里面获取键为list的值的数据
        listData = jsonDataId["list"]
        # 批量获取id
        # 循环遍历该列表数据(该数据列表里面是字典形式[{},{},{},{}]...)
        for data in listData:
            idList.append(data["ID"])

    # 获取企业详情数据
    url = "http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById"

    for detail_id in idList:
        data = {
            "id": detail_id
        }
        detail_data = requests.post(url=url, headers=headers, data=data).json()
        print(detail_data)
        all_data_list.append(detail_data)
    # 永久储存数据
    fp = open("./yaojianju.json", "w", encoding="utf-8")
    json.dump(all_data_list, fp, ensure_ascii=False)
    print("*************** 爬取结束 ******************")
